package com.freecycle.sg.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.freecycle.sg.sqlite.Freebie.Freebies;

import android.content.ContentValues;
import android.util.Log;

public class JSONUtils {

	private static final String TAG = "JSONUtils";

	static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

	/**
	 * Helper methods to get primitive data from {@link JSONObject}
	 * 
	 * @param key
	 * @param json
	 * @return respective data type
	 */
	public static String getStringIfExist(String key, JSONObject json) {
		try {
			String value = json.getString(key);
			return value;
		} catch (JSONException e) {
			Log.v(TAG, "getStringIfExist(): error while getting key " + key
					+ " " + e);
		}
		return "";
	}

	public static int getIntIfExist(String key, JSONObject json) {
		try {
			return json.getInt(key);
		} catch (JSONException e) {
			Log.v(TAG, "getIntIfExist(): error while getting key " + key + " "
					+ e);
		}
		return 0;
	}

	public static double getDoubleIfExist(String key, JSONObject json) {
		try {
			return json.getDouble(key);
		} catch (Exception e) {
			Log.v(TAG, "getDoubleIfExist(): error while getting key " + key
					+ " " + e);
		}
		return 0;
	}

	public static Date getDateIfExist(String key, JSONObject json) {
		try {
			String date = json.getString(key);
			return sdf.parse(date);
		} catch (JSONException e) {
			Log.v(TAG, "getDateIfExist(): error while getting key " + key + " "
					+ e);
		} catch (ParseException e) {
			Log.v(TAG, "getDateIfExist(): error parsing date " + key + " " + e);
		}
		return null;
	}

	public static String[] getStringArrayIfExist(String key, JSONObject json) {
		try {
			JSONArray arr = json.getJSONArray(key);
			int length = arr.length();
			if (length == 0) {
				return null;
			} else {
				String[] values = new String[length];
				for (int i = 0; i < length; i++) {
					values[i] = arr.getString(i);
				}
				return values;
			}
		} catch (JSONException e) {
			Log.v(TAG, "getStringArrayIfExist(): error while getting key "
					+ key + " " + e);
		}
		return null;
	}

	public static HashMap<String, String> convertHashMap(JSONObject jo) {
		HashMap<String, String> map = new HashMap<String, String>();
		try {
			for (int i = 0; i < jo.names().length(); i++) {
				String key = jo.names().getString(i);
				String value = jo.getString(key);
				map.put(key, value);
			}
		} catch (JSONException e) {
			Log.v(TAG, "_convertHashMap: error while getting key", e);
		}
		return map;
	}
	
	public static ContentValues buildFreebieCv(JSONObject obj) {
		ContentValues cv = new ContentValues();
		cv.put(Freebies.FREEBIE_ID, getStringIfExist("id", obj));
		cv.put(Freebies.ADDED, getStringIfExist("added", obj));
		cv.put(Freebies.LATITUDE, getStringIfExist("latitude", obj));
		cv.put(Freebies.LONGITUDE, getStringIfExist("longitude", obj));
		cv.put(Freebies.FORMATTED_ADDRESS, getStringIfExist("formatted_address", obj));
		cv.put(Freebies.NAME, getStringIfExist("name", obj));
		cv.put(Freebies.TITLE, getStringIfExist("title", obj));
		cv.put(Freebies.DESCRIPTION, getStringIfExist("description", obj));
		cv.put(Freebies.CATEGORY, getStringIfExist("category", obj));
		cv.put(Freebies.PHONE, getStringIfExist("phone", obj));
		return cv;
	}

}
